# this script produces Figure 4 in the paper (plots of regression residuals)

pacman::p_load(tidyverse, haven, patchwork)

# load the data set for the regression
reg_data <- read_dta("Additional Data/Delo_UP_Panel_DS_h0406.dta") %>%
  filter(year == 2004)

# regress Orange on Gray and controls
orange_mod <- lm(orange_po ~ gray_po + lemp + tfp + privatized + sec1 + sec2 +
                   sec4 + sec5 + sec6 + sec7 + sec8 + sec9 + sec10,
                 reg_data)

# regress vote for Yushchenko on Gray and controls
yudec_gray_mod <- lm(YuDec ~ gray_po + lemp + tfp + privatized + sec1 + 
                       sec2 + sec4 + sec5 + sec6 + sec7 + sec8 + sec9 + sec10,
                     reg_data)

# regress Gray on Orange and controls
gray_mod <- lm(gray_po ~ orange_po + lemp + tfp + privatized + sec1 + sec2 +
                   sec4 + sec5 + sec6 + sec7 + sec8 + sec9 + sec10,
                 reg_data)

# regress vote for Yushchenko on Orange and controls
yudec_orange_mod <- lm(YuDec ~ orange_po + lemp + tfp + privatized + sec1 + 
                       sec2 + sec4 + sec5 + sec6 + sec7 + sec8 + sec9 + sec10,
                     reg_data)

# combine residuals from for models into a data frame
res_data <- tibble(res_orange_on_gray = resid(orange_mod),
                   res_yudec_on_gray = resid(yudec_gray_mod),
                   res_gray_on_orange = resid(gray_mod),
                   res_yudec_on_orange = resid(yudec_orange_mod))

# regress residuals from Orange on Gray on residuals from Yushchenko vote on Gray
# extract coefficients and standard errors
reg_coef_or <- round(summary(lm(res_orange_on_gray ~ res_yudec_on_gray, 
                          res_data))$coefficients[2, 1], 3)
reg_se_or <- round(summary(lm(res_orange_on_gray ~ res_yudec_on_gray, 
                          res_data))$coefficients[2, 2], 3)

# regress residuals from Gray on Orange on residuals from Yushchenko vote on Orange
# extract coefficients and standard errors
reg_coef_gr <- round(summary(lm(res_gray_on_orange ~ res_yudec_on_orange, 
                                res_data))$coefficients[2, 1], 3)
reg_se_gr <- round(summary(lm(res_gray_on_orange ~ res_yudec_on_orange, 
                              res_data))$coefficients[2, 2], 3)

# plot residuals from Orange on Gray and residuals from Yushchenko vote on Gray
# overlaid with the regression line
# ggplot(res_data, aes(res_yudec_on_gray, res_orange_on_gray)) +
#   geom_point() +
#   geom_smooth(method = "lm", color = "black", se = F) +
#   annotate("text", x = -0.5, y = -0.75, 
#            label = paste0("Reg coef: ", reg_coef_or, 
#                           ", SE = ", reg_se_or)) +
#   labs(x = "Residualized vote for Yushchenko", 
#        y = "Residualized Orange control") +
#   theme_bw()
# ggsave("Results/avplot_r_Orange.pdf", width = 7, height = 5)

# plot residuals from Gray on Orange and residuals from Yushchenko vote on Orange
# overlaid with the regression line
ggplot(res_data, aes(res_yudec_on_orange, res_gray_on_orange)) +
  geom_point() +
  geom_smooth(method = "lm", color = "black", se = F) +
  annotate("text", x = -0.52, y = -0.75, 
           label = paste0("Reg coef: ", reg_coef_gr, 
                          ", SE = ", reg_se_gr)) +
  labs(x = "Residualized vote for Yushchenko", 
       y = "Residualized Gray control") +
  theme_bw()
ggsave("Results/Figure_4.pdf", width = 7, height = 5)

